/*************************************************************
 * 
 * CopyRight(c)2007, http://www.moorwind.com/
 * email:ezhung@gmail.com
 * for free, but a email permission would be nice
 * 
 ************************************************************/
package kono.utils
{
	//二维数组， Two-Dimensional Array, built on a linear array 
	public class D2Array
	{
		private var _width:uint;
		private var _height:uint;
		
		private var _array:Array;
		
		//@param width:二维数组宽度，the colum of the 2D array
		//@param height:二维数组长度，the row of the 2D array
		public function D2Array(width:uint = 1, height:uint = 1)
		{
			_array = new Array((_width = width) * (_height = height));
		}
		
		//取得数组大小， get the total number of the 2D array's cell
		public function get size():uint
		{
			return _width * _height;
		}
		
		public function get width():uint
		{
			return _width;
		}
		
		public function get height():uint
		{
			return _height;
		}
		
		//取得一行值, get the row at the given index
		public function getRow(row:uint):Array
		{
			var start:uint = row * _width;
			return _array.slice(start, start + _width);
		}
		
		//取得一列值， get the colum at the fiven index
		public function getColum(colum:uint):Array
		{
			var col:Array = new Array();
			for(var i:uint = 0; i < _height; i++)
			{
				col[i] = _array[i*_width + colum];
			}
			return col;
		}
		
		//返回指定坐标（x，y）位置的数据，read the value at the given point (x, y)
		public function getItem(x:uint, y:uint):*
		{
			return _array[uint(_width*y + x)];
		}
		
		//设定指定位置（x，y）的数值. set the value at the given point (x, y)
		public function addItem(x:uint, y:uint, data:*):void
		{
			_array[uint(_width*y + x)] = data;
		}
		
		//增加一行， append a row;
		public function appendRow(row:Array):void
		{
			row.length = _width;
			_array = _array.concat(row);
			_height++;
		}
		
		//增加一列， append a colum
		public function appendColum(colum:Array):void
		{
			colum.length = _height;
			for(var i:uint = 0; i < _width; i++)
			{
				_array = _array.splice(i*_width + _width + i, 0, colum[i]);
			}
			_width ++;
		}
		
		//填充数据， fill each of the 2D array's cell with the given data
		public function fill(data:*):void
		{
			var s:uint = _width * _height;
			for(var i:uint = 0; i < s; i++)
			{
				_array[i] = data;
			}
		}
		
		//重设数组大小，resize the 2D array to match the given width and height 
		public function resize(width:uint, height:uint):void
		{
			var temp:Array = _array.concat();	
			_array = new Array(width * height);
			
			var min_width:uint = width < _width ? width : _width;
			var min_height:uint = height < _height ? height : _height;
			
			var i:uint, j:uint; 
			for(i = 0; i < min_width; i++)
			{
				for(j=0; j< min_height; j++)
				{
					_array[uint(width*j + i)] = temp[uint(_width*j + i)];
				} 
			}
			_width = width;
			_height = height;
		}
						
		//判断数组是否包含某个数据， chack if the 2D array contains the given data
		public function contains(data:*):Boolean
		{
			var s:uint = _width * _height;
			for(var i:uint; i < s; i++)
			{
				if(_array[i] == data)
				{
					return true;
				}
			}
			return false;
		}
		
		//返回一维数组, convert the 2D array to a linear array
		public function toArray():Array
		{
			return _array;
		}
		
		//返回2维数组信息， print the information of the 2D array
		public function toString():String
		{
			return ("[2-Dimensional Array, width=" + _width + " height=" + _height +" size=" + _width*_height +"]");
		}
	}
}